﻿
from PySide6.QtCore import Qt, QSettings, Slot, Signal
from PySide6.QtWidgets import QDialog

from Devices.Andlink.Forms.SendCoAPWindow_UI import Ui_FormSendCoAP

from Funcs.Config import Config

class SendCoAPWindow(QDialog):
    SendCoAPTriggerd = Signal(str, str, str)

    def __init__(self, parent = None):
        super().__init__(parent)

        self.__ui = Ui_FormSendCoAP()
        self.__ui.setupUi(self)
        self.__DeviceDisplayName = ""

        flags = self.windowFlags()
        self.setWindowFlags(flags & ~Qt.WindowContextHelpButtonHint)

    def SetDeviceName(self, deviceDisplayName):
        __DeviceDisplayName = deviceDisplayName

        s = QSettings(Config.ConfigIniPath(), QSettings.IniFormat)
        s.beginGroup("UI")
        s.beginGroup(__DeviceDisplayName)
        s.beginGroup("SendCoAPWindow")

        for i in range(1,9):
            if not s.value("url" + str(i)) is None:
                lineEdit = eval("self._SendCoAPWindow__ui.lineEdit_url" + str(i))
                lineEdit.setText(s.value("url" + str(i)))
            if not s.value("Command" + str(i)) is None:
                plainTextEdit = eval("self._SendCoAPWindow__ui.plainTextEdit_Command" + str(i))
                plainTextEdit.setPlainText(s.value("Command" + str(i)))

        s.endGroup()
        s.endGroup()
        s.endGroup()

    def SetDeviceIP(self, ip):
        self.setWindowTitle("发送CoAP命令" + (":" + ip if (0 == len(ip)) else ""))
        self.__ui.lineEdit_IP1.setText(ip)
        self.__ui.lineEdit_IP2.setText(ip)
        self.__ui.lineEdit_IP3.setText(ip)
        self.__ui.lineEdit_IP4.setText(ip)
        self.__ui.lineEdit_IP5.setText(ip)
        self.__ui.lineEdit_IP6.setText(ip)
        self.__ui.lineEdit_IP7.setText(ip)
        self.__ui.lineEdit_IP8.setText(ip)

    def saveCommand(self, i):
        s = QSettings(Config.ConfigIniPath(), QSettings.IniFormat)
        s.beginGroup("UI")
        s.beginGroup(self.__DeviceDisplayName)
        s.beginGroup("SendCoAPWindow")
        lineEdit = eval("self._SendCoAPWindow__ui.lineEdit_url" + str(i))
        plainTextEdit = eval("self._SendCoAPWindow__ui.plainTextEdit_Command" + str(i))
        s.setValue("url" + str(i), lineEdit.text())
        s.setValue("Command" + str(i), plainTextEdit.toPlainText())
        s.endGroup()
        s.endGroup()
        s.endGroup()

    @Slot()
    def on_pushButton_Send1_clicked(self):
        self.saveCommand(1)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP1.text(),"/" + self.__ui.lineEdit_url1.text(), self.__ui.plainTextEdit_Command1.toPlainText())

    @Slot()
    def on_pushButton_Send2_clicked(self):
        self.saveCommand(2)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP2.text(), "/" + self.__ui.lineEdit_url2.text(), self.__ui.plainTextEdit_Command2.toPlainText())
                                            
    @Slot()                                 
    def on_pushButton_Send3_clicked(self):  
        self.saveCommand(3)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP3.text(), "/" + self.__ui.lineEdit_url3.text(), self.__ui.plainTextEdit_Command3.toPlainText())
                                            
    @Slot()                                 
    def on_pushButton_Send4_clicked(self):  
        self.saveCommand(4)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP4.text(), "/" + self.__ui.lineEdit_url4.text(), self.__ui.plainTextEdit_Command4.toPlainText())
                                            
    @Slot()                                 
    def on_pushButton_Send5_clicked(self):  
        self.saveCommand(5)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP5.text(), "/" + self.__ui.lineEdit_url5.text(), self.__ui.plainTextEdit_Command5.toPlainText())
                                            
    @Slot()                                 
    def on_pushButton_Send6_clicked(self):  
        self.saveCommand(6)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP6.text(), "/" + self.__ui.lineEdit_url6.text(), self.__ui.plainTextEdit_Command6.toPlainText())
                                            
    @Slot()                                 
    def on_pushButton_Send7_clicked(self):  
        self.saveCommand(7)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP7.text(), "/" + self.__ui.lineEdit_url7.text(), self.__ui.plainTextEdit_Command7.toPlainText())
                                            
    @Slot()                                 
    def on_pushButton_Send8_clicked(self):  
        self.saveCommand(8)
        self.SendCoAPTriggerd.emit(self.__ui.lineEdit_IP8.text(), "/" + self.__ui.lineEdit_url8.text(), self.__ui.plainTextEdit_Command8.toPlainText())
        
    @Slot()
    def on_closeButton_clicked(self):
        self.reject()
